﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.DataAccess.Interfaces;
using Core.Model;
using Core.Logic.Interfaces;

namespace Core.Logic.Implementations
{
    public class QuestionListLogicProvider: LogicProvider<QuestionList>,IQuestionListLogicProvider
    {
        private ITagDataProvider tagDataProvider;
        public QuestionListLogicProvider(IModelDataProvider<QuestionList> modelDataProvider) 
            : base(modelDataProvider)
        {
            tagDataProvider = DataProviderFactory.GetTagDataProvider();
        }

        public override QuestionList Update(QuestionList entity) 
        {
            entity.Tags = CheckForTags(entity.Tags); 
            foreach (var tag in entity.Tags.Where(tag => !tag.QuestionLists.Contains(entity)))
            {
                tag.QuestionLists.Add(entity);
                tagDataProvider.Update(tag);
            }
           return  base.Update(entity);
        }

        public override QuestionList Save(QuestionList entity)
        {
            entity.Tags = CheckForTags(entity.Tags);
            foreach (var tag in entity.Tags.Where(tag => !tag.QuestionLists.Contains(entity)))
            {
                tag.QuestionLists.Add(entity);
                tagDataProvider.Update(tag);
            }
            return base.Save(entity);
        }


        public override void Delete<Guid>(Guid id)
        {
            var entity = Get(id);
            entity.Tags = CheckForTags(entity.Tags);
            foreach (var tag in entity.Tags.Where(tag => !tag.QuestionLists.Contains(entity)))
            {
                tag.QuestionLists.Remove(entity);
                tagDataProvider.Update(tag);
            }
            base.Save(entity);
        }

        private IList<Tag> CheckForTags(IList<Tag> tags)
        {
            var tp = DataProviderFactory.GetTagDataProvider();
            for (var i = 0; i < tags.Count() - 1;i++ )
            {
                tags[i] = tp.GetTagByName(tags[i].TagTitle) ?? tags[i]; 
            }
            return tags;
        }

       
        #region Implementation of IQuestionListLogicProvider

        public void AddTagToQuestionList(QuestionList questionList, string tagName)
        {
            var tag = tagDataProvider.GetTagByName(tagName) ?? tagDataProvider.Save(new Tag(tagName));
            questionList.Tags.Add(tag);
            tag.QuestionLists.Add(questionList);
            ModelDataProvider.Update(questionList);
            tagDataProvider.Update(tag);
        }

        public void RemoveTagFromQuestionList(QuestionList questionList, string tagName)
        {
            var tag = tagDataProvider.GetTagByName(tagName);
            questionList.Tags.Remove(tag);
            tag.QuestionLists.Remove(questionList);
            ModelDataProvider.Update(questionList);
            tagDataProvider.Update(tag);
        }

        public IList<QuestionList> GetQuestionListsByTag(string tagName)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
